package com.car.account.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.car.account.entity.Stores;
import com.car.account.entity.StoresRole;
import com.car.account.exception.AccountException;
import com.car.account.service.StoresRoleService;
import com.car.account.service.StoresService;
import com.car.commons.core.model.Result;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/storesRole")
@Api(tags = "门店身份")
public class StoresRoleController {

    @Resource
    private StoresService storesService;

    @Resource
    private StoresRoleService storesRoleService;

    @GetMapping("/selectRole.md")
    @ApiOperation(value = "店长查询门店所有身份")
    public Result<List<StoresRole>> selectRole() throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentClerkId = JwtUtil.getClerkId(token);
        //判断是否存在店员账号
        Stores currentClerk = storesService.getById(currentClerkId);
        if (currentClerk == null || currentClerk.getClerkState() == 2)
            throw new AccountException(3001,"店员账号不存在");
        //判断当前门店账号状态
        if (currentClerk.getClerkState() == 1) throw new AccountException(3002,"店员账号已被禁用");
        List<StoresRole> storesRoles = storesRoleService.list(new QueryWrapper<StoresRole>());
        return Result.getListResult(StoresRole.class).setData(storesRoles).setMsg("店长查询门店所有身份成功");
    }

}

